package com.youlu.campus.web.clock;

import com.alibaba.fastjson.JSON;
import com.youlu.campus.entity.clock.dto.UserClockQueryDTO;
import com.youlu.campus.entity.clock.dto.UserClockSaveDTO;
import com.youlu.campus.service.clock.UserClockService;
import com.youlu.campus.web.common.base.RequestContext;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * 用户连续打卡控制器
 * 处理用户打卡、每周打卡记录查询及每月打卡记录查询等操作
 *
 * @author : [dengbo]
 * @createTime : [2023/5/19 17:39]
 */
@Slf4j
@RestController
@RequestMapping("/mini-app/")
public class UserClockController {

    @Autowired
    private UserClockService userClockService;


    /**
     * 用户打卡接口
     * 处理用户的打卡操作，记录打卡时间及相关信息
     * @param request 打卡信息DTO，包含打卡相关参数
     * @return 打卡操作结果
     */
    @PostMapping("/user/lx/clock")
    public QResult userClock(@Validated @RequestBody UserClockSaveDTO request) {
        String userId = RequestContext.getUserId();
        log.info("userId:{},request param:{}", userId, JSON.toJSONString(request));
        request.setUserId(userId);
        request.setUserPO(RequestContext.getCurUser());
        request.setAppId(RequestContext.getAppId());
        request.setClockDate(LocalDateTime.now());
        return QResult.success(userClockService.userClock(request));
    }


    /**
     * 用户每周打卡列表接口
     * 查询当前用户一周内的打卡记录信息
     * @param request 打卡查询DTO，可包含时间范围等筛选条件
     * @return 每周打卡记录数据
     */
    @PostMapping("/user/clock/week/query")
    public QResult userDayClockQuery(@Validated @RequestBody UserClockQueryDTO request) {
        String userId = RequestContext.getUserId();
        log.info("userId:{},request param:{}", userId, JSON.toJSONString(request));
        request.setUserId(userId);
        request.setClockDate(LocalDateTime.now());
        return QResult.success(userClockService.queryUserWeekClockInfo(request));
    }

    /**
     * 用户每月打卡列表接口
     * 查询当前用户一个月内的打卡记录信息
     * @param request 打卡查询DTO，可包含时间范围等筛选条件
     * @return 每月打卡记录数据
     */
    @PostMapping("/user/clock/month/query")
    public QResult userMonthClockQuery(@Validated @RequestBody UserClockQueryDTO request) {
        String userId = RequestContext.getUserId();
        log.info("userId:{},request param:{}", userId, JSON.toJSONString(request));
        request.setUserId(userId);
        return QResult.success(userClockService.queryUserMonthClockInfo(request));
    }
}